package tiled.gwt;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;


public class Map implements Serializable{
	private static final long serialVersionUID = -6433036696253857126L;

	public Map(){}
	
    public static final int ORIENTATION_ORTHOGONAL = 1;
    public static final int ORIENTATION_ISOMETRIC = 2;
    public static final int ORIENTATION_HEXAGONAL = 4;
    public static final int ORIENTATION_SHIFTED = 5;

	public int orientation=ORIENTATION_ORTHOGONAL;
	public int width=0;
	public int height=0;
	public int tilewidth=0;
	public int tileheight=0;
	
	public HashMap<Integer,TileSet> tileSets = new HashMap<Integer,TileSet>();
	public HashMap<String,ObjectGroup> objectGroups = new HashMap<String,ObjectGroup>();
	public HashMap<String,MapLayer> mapLayers = new HashMap<String,MapLayer>();
	
	public void loadImages(TiledScreen ts){
		Collection<TileSet> c = tileSets.values();
		Iterator<TileSet> itr = c.iterator();
		 
		while (itr.hasNext()) {
	    	TileSet tileSet = (TileSet) itr.next();
	    	ts.loadImage(tileSet.source);
	    }
	}
	
	public void render(TiledScreen ts){
		Collection<MapLayer> c=mapLayers.values();
		Iterator<MapLayer> itr = c.iterator();
		while (itr.hasNext()) {
	        MapLayer mapLayer = (MapLayer) itr.next();
	        
	        for(int y=0;y<mapLayer.height;y++){
		        for(int x=0;x<mapLayer.width;x++){

		        	Tile tile=mapLayer.tiles[x][y];
		        	TileSet tileSet=tileSets.get(tile.Tn);
		        	if((tile.n!=-1)&&(tile.Tn!=-1)){
		        		int sx=(tile.n%tileSet.tilesPerRow)*(tileSet.tileWidth+tileSet.spacing)+tileSet.margin;
		        		int sy=(tile.n/tileSet.tilesPerRow)*(tileSet.tileHeight+tileSet.spacing)+tileSet.margin;
			        	ts.putImage(tileSet.source, sx, sy, tileSet.tileWidth, tileSet.tileHeight, x*tilewidth, y*tileheight, tilewidth, tileheight);
		        	}
		        }
	        }
	    }
	}

}
